package com.lingchen.boot.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lingchen.boot.gof.aop.Log;
import com.lingchen.boot.mapper.LofterDataMapping;
import com.lingchen.boot.mapper.SeedMapping;
import com.lingchen.boot.pojo.LofterData;
import com.lingchen.boot.pojo.Seed;
import com.lingchen.boot.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lingChen
 * @Date: 2022/5/13
 */
@Slf4j
@Component
public class LofterJob {

    @Autowired
    SeedMapping seedMapping;
    @Autowired
    LofterDataMapping lofterDataMapping;
    private static final String LOFTER_URL ="http://api.lofter.com/oldapi/post/detail.api?product=lofter-iphone-6.24.0";
    private static final Map HEADERS = new HashMap(){{
        put("Content-Type", "application/x-www-form-urlencoded");
    }};



    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void startJob() throws Exception{
        List<Seed> seeds = seedMapping.selectAll();

        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        Date day = DateUtils.parseDate(format, "yyyy-MM-dd HH:mm:ss");
        for (Seed seed : seeds) {
            LofterData ld = this.getDescByBody(seed.getBody(),day);
            LofterData old = new LofterData();
            old.setDay(ld.getDay());
            old.setLofterId(ld.getLofterId());
            List<LofterData> oldList = lofterDataMapping.select(old);
            if(null == oldList || oldList.isEmpty()){
                lofterDataMapping.insert(ld);
            }
        }
    }

    private LofterData getDescByBody(String body ,Date day){
        String s = HttpClientUtil.sendPostRequest(LOFTER_URL, body, HEADERS);
        LofterData ld = new LofterData();
        ld.setDay(day);
        ld.setLofterId(body);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject response = jsonObject.getJSONObject("response");
        JSONArray posts = response.getJSONArray("posts");
        for (Object post : posts) {
            JSONObject postObject = (JSONObject) post;
            postObject = postObject.getJSONObject("post");
            String title = postObject.getString("title");
            ld.setTitle(title);
            JSONObject postCount = postObject.getJSONObject("postCount");
            //获取评论数
            Long responseCount = postCount.getLong("responseCount");
            //获取点赞数
            Long favoriteCount = postCount.getLong("favoriteCount");
            //获取转发数
            Long shareCount = postCount.getLong("shareCount");
            //获取订阅
            Long subscribeCount = postCount.getLong("subscribeCount");
            //获取热度
            Long postHot = postCount.getLong("postHot");

            ld.setResponseCount(responseCount);
            ld.setFavoriteCount(favoriteCount);
            ld.setShareCount(shareCount);
            ld.setSubscribeCount(subscribeCount);
            ld.setPostHot(postHot);
        }
        return ld;
    }

}
